#include "usys.h"
#include "xtime.h"
#include "str.h"
#include "fs.h"
#include <errno.h>
#include <sys/param.h>
#include <sys/mount.h>
#ifndef __APPLE__
#include <sys/vfs.h>
#endif
#include <stdio.h>
#include <string.h>
#include <map>

namespace log4x
{
usys::usys()
{
}

usys::~usys()
{
}

int
usys::hdd_info(hdd_info_s& info)
{
    return -1;
}

int
usys::hdd_stat(const string& path, hdd_stat_s& stat)
{
    string rootPath = path;
    fs::path_to_unix(rootPath);

    struct statfs buf;
    if (statfs(rootPath.c_str(), &buf))
    {
        return -1;
    }

    switch (buf.f_type)
    {
    case 0xEF51:
    case 0xEF53:
        stat.format = "EXT";
        break;

    case 0x4d44:
        stat.format = "FAT";
        break;

    case 0x5346544e:
        stat.format = "NTFS";
        break;

    default:
        stat.format = "UNKNOWN";
        break;
    }

    stat.total = buf.f_blocks * buf.f_bsize / 1024;
    stat.free  = buf.f_bavail * buf.f_bsize / 1024;
    stat.used  = (buf.f_blocks - buf.f_bfree) * buf.f_bsize / 1024;

    stat.used_percent = stat.used / (double)stat.total * 100;
    stat.free_percent = stat.free / (double)stat.total * 100;
    return 0;
}

}
